package com.aconcagua.samples.charts.controller.commands {
  import com.aconcagua.samples.charts.controller.gestures.CreateSliderGesture;
  import com.aconcagua.samples.charts.service.ChartServiceDelegate;
  import com.aconcagua.samples.charts.state.ApplicationState;
  import com.aconcagua.samples.charts.state.model.Series;
  import com.aconcagua.samples.charts.state.model.Slider;
  import com.aconcagua.samples.charts.state.model.model;
  
  import mx.collections.ArrayCollection;
  import mx.rpc.events.ResultEvent;
  
  public class CreateSliderCommand extends ResponderCommand {
    private var _chartService: ChartServiceDelegate;
    private var _applicationState: ApplicationState;

    private var _leftBoundary: Date;
    private var _rightBoundary: Date;

    public function CreateSliderCommand(state: ApplicationState, service: ChartServiceDelegate) {
      super(onResult);
      _applicationState = state;
      _chartService = service;
    }

    public function getSliderData(gesture: CreateSliderGesture): void {
      _leftBoundary = gesture.leftBoundary;
      _rightBoundary = gesture.rightBoundary;

      _chartService.getData(_leftBoundary, _rightBoundary).invoke(this);
    }
    
    private function onResult(event: ResultEvent): void {
      var elements: ArrayCollection = event.result as ArrayCollection;

      _applicationState.model::setSlider(
        new Slider(elements.getItemAt(0) as Series)
      );
    }
  }
}